package cn.ekuma.epos.finance;

import java.awt.BorderLayout;

import javax.swing.JPanel;

import com.openbravo.bean.CloseCash;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.loader.query.QBFCompareEnum;
import com.openbravo.data.loader.query.QBFParameter;
import com.openbravo.data.loader.query.QBFParameters;
import com.openbravo.data.loader.ui.JQBFParametersPanel;
import com.openbravo.format.Formats;
import com.openbravo.pos.base.AppView;
import com.openbravo.pos.base.BeanFactoryException;
import com.openbravo.pos.panels.event.TimeCycleChangedEvent;
import cn.ekuma.epos.db.table.I_CloseCash;
import cn.ekuma.epos.finance.model.CashBookTableModel;
import cn.ekuma.epos.module.ACC_UI_Swing_Model;
import cn.ekuma.epos.panel.AbstractSingleReadOnlyJModelFinishingPanel;

public class JCashBookFinishing extends AbstractSingleReadOnlyJModelFinishingPanel<CloseCash>{

	QBFParameters qbfParameters;
	QBFParameter dateStart,dateEnd;
	public JCashBookFinishing() {
		super(new CashBookTableModel());
	}

	@Override
	public String getTitle() {
		return "label.CashBookFinishing";
	}

	@Override
	public Object createValue() throws BasicException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void timeCycleChanged(TimeCycleChangedEvent e) {
		if (e.getStartDate() != null)
			jTxtStartDate.setText(Formats.TIMESTAMP.formatValue(e.getStartDate()));
		else
			jTxtStartDate.setText(null);
		if (e.getEndDate() != null)
			jTxtEndDate.setText(Formats.TIMESTAMP.formatValue(e.getEndDate()));
		else
			jTxtEndDate.setText(null);
		reQuery();
	}

	@Override
	protected void initFinishPanel(AppView app) throws BeanFactoryException {
		qbfParameters=new QBFParameters();
		dateStart=new QBFParameter(I_CloseCash.DATESTART);
		dateEnd=new QBFParameter(I_CloseCash.DATESTART);
		qbfParameters.and(dateStart).and(dateEnd);
		
		JCashBookLine cashBookLine=new JCashBookLine();
		cashBookLine.init(mode.getDLERP());
		linkTabPanel.add("line", cashBookLine);
	}

	@Override
	public void reQuery() {
		// Date range
		Object startdate=null;
		Object enddate=null;
		try {
			startdate = Formats.TIMESTAMP.parseValue(jTxtStartDate.getText());
			enddate =Formats.TIMESTAMP.parseValue(jTxtEndDate.getText());
		} catch (BasicException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		dateStart.setQbf((startdate == null) ? QBFCompareEnum.COMP_NONE
				: QBFCompareEnum.COMP_GREATEROREQUALS);
		dateStart.setValue(startdate);
		dateEnd.setQbf((enddate == null) ? QBFCompareEnum.COMP_NONE
				: QBFCompareEnum.COMP_LESS);
		dateEnd.setValue(enddate);
		try {
			tableModel.resert(dlSales.query(CloseCash.class, qbfParameters));
		} catch (BasicException ex) {
			new MessageInf(ex).show(this);
		}
	}

	@Override
	protected void configNavigator(JPanel jNavigatorToolBar, JPanel jNavigator) {
		JPanel panel_3 = new JPanel();
		jNavigator.add(panel_3, "QBFPanel");
		panel_3.setLayout(new BorderLayout(0, 0));
		JQBFParametersPanel qbfPanel=new JQBFParametersPanel(){
			@Override
			public void doQBFParametersFilter(QBFParameters paras) {
				try {
					tableModel.resert(dlSales.query(CloseCash.class, paras));
				} catch (BasicException e) {
					new MessageInf(e).show(this);
				}
			}
        };
        qbfPanel.setShowFieldCount(6);
        qbfPanel.setFields(CashBookTableModel.getQBFFields());
        panel_3.add(qbfPanel, BorderLayout.CENTER);
	}

	@Override
	public String getModelName() {
		return ACC_UI_Swing_Model.MODEL_NAME;
	}
	
	
	
	
}
